package com.base.admin.exception;


import com.base.admin.vo.ResultBean;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author rqq
 */
@RestControllerAdvice
@Component
public class GlobalExceptionHandler {

    @ExceptionHandler(BizException.class)
    public ResultBean<Void> exceptionHandler(BizException ex) {
        return ResultBean.error(ex.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public ResultBean<Void> handler(BindException ex) {
        String errorMsg = ex.getBindingResult().getFieldError().getDefaultMessage();
        return ResultBean.error(errorMsg);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultBean<Void> handler(MethodArgumentNotValidException ex) {
        String errorMsg = ex.getBindingResult().getFieldError().getDefaultMessage();
        return ResultBean.error(errorMsg);
    }
}
